import { createStore } from 'vuex'
const types = {
    SET_AUTHENTICATED: "SET_AUTHENTICATED",
    SET_USER: "SET_USER",
}
const state = {
    isAuthenticated: false, // 是否认证通过
    user: {}
};
const actions = {
    setAuthenticated: ({commit}, isAuthenticated) =>{
        // console.log("setAuthenticated action called with isAuthenticated:", isAuthenticated);
        commit(types.SET_AUTHENTICATED,isAuthenticated);
    },
    setUser: ({ commit }, user) => {
        commit(types.SET_USER,user);
    },
    clearCurrentState: ({ commit }) =>{
        commit(types.SET_AUTHENTICATED, false);
        commit(types.SET_USER, null);
    }

};
const mutations = {
    [types.SET_AUTHENTICATED](state, isAuthenticated){
        if(isAuthenticated) state.isAuthenticated = isAuthenticated;
        else state.isAuthenticated = false;
        console.log(state.isAuthenticated);
    },
    [types.SET_USER](state, user){
        if(user) state.user = user;
        else state.user = {};
    }

    
};
const getters = {
    isAuthenticated: state => state.isAuthenticated,
    user: state => state.user
};
export default createStore({
    state,
    actions,
    mutations,
    getters
})